$(document).ready(function () {
    selectMenu("twoMenu-id|studyMenu-id");

    var table = $('#dynamic-table').DataTable({
        "serverSide": true,// 服务器端分页处理
        "bAutoWidth": false,
        "bProcessing": true, //DataTables载入数据时，是否显示‘进度’提示
        "aLengthMenu": [5, 10, 20, 50], //更改显示记录数选项
        "iDisplayLength": 10, //默认显示的记录数
        "searching": false,
        "bInfo": true, //是否显示页脚信息，DataTables插件左下角显示记录数
        "sPaginationType": "full_numbers", //详细分页组，可以支持直接跳转到某页
        "bSort": false, //是否启动各个字段的排序功能
        "language": {
            url: '/static/i18n/chinese.json'
        },
        fnDrawCallback: function (table) {
            var pageNo = (table.oAjaxData.start / table.oAjaxData.length) + 1;
            var oTable = $("#dynamic-table").dataTable();
            $.skipPage(pageNo, oTable);
        },
        columns: [
            {"data": "userCode", "defaultContent": ""},
            {"data": "userName", "defaultContent": ""},
            {"data": "averageScore", "defaultContent": ""},
            {"data": "counselorScore", "defaultContent": ""},
            {"data": "awardCount", "defaultContent": ""},
            {"data": "competitionCount", "defaultContent": ""},
            {"data": "lectureCount", "defaultContent": ""},
            {"data": "readBookCount", "defaultContent": ""},
            {"data": "failedCount", "defaultContent": ""},
            {"data": "truancyCount", "defaultContent": ""},
            {"data": "studyTime", "defaultContent": ""},
            {"data": "favoriteSubject", "defaultContent": ""},
            {"data": "remark", "defaultContent": ""}
        ],
        ajax: function (data, callback, settings) {
            var param = {};
            param.pageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
            param.start = data.start;//开始的记录序号
            param.page = (data.start / data.length) + 1;//当前页码
            param.draw = data.draw;
            var x = $("#queryForm").serializeArray();
            $.each(x, function (i, field) {
                param[field.name] = field.value;
            });
            $.ajax({
                type: "post",
                url: "/study/list",
                cache: false,	//禁用缓存
                data: param,	//传入已封装的参数
                dataType: "json",
                success: function (result) {
                    var returnData = {};
                    returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                    returnData.recordsTotal = result.total;
                    returnData.recordsFiltered = result.total;//后台不实现过滤功能，每次查询均视作全部结果
                    returnData.data = result.rows;
                    callback(returnData);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    toastr.error("查询失败");
                }
            });
        }
    });

    //查询按钮
    $("#submitQuery").on('click', function () {
        table.ajax.reload();
    });

    //重置按钮
    $("#resetQuery").on('click', function () {
        $('#queryForm')[0].reset();
        table.ajax.reload();
    });
});